package com.lc.YYGH.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lc.YYGH.hosp.mapper.ProfessionMapper;
import com.lc.YYGH.hosp.repository.ProfessionRepository;
import com.lc.YYGH.hosp.service.ProfessionService;
import com.lc.YYGH.model.cmn.Dict;
import com.lc.YYGH.model.hosp.Schedule;
import com.lc.YYGH.model.office.Lawer;
import com.lc.YYGH.model.office.Profession;
import com.lc.YYGH.vo.office.ProfessionQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
@Service
public class ProfessionServiceImpl implements ProfessionService {
    @Autowired
    private ProfessionRepository professionRepository;
    @Autowired
    private ProfessionMapper professionMapper;
    @Override

    public void save(Map<String, Object> map) {
        Profession profession= JSONObject.parseObject(JSONObject.toJSONString(map),Profession.class);
        Profession profession1=professionRepository.findByLawercodeAndProfessioncode(profession.getLawercode(),profession.getProfessioncode());
        if(null!=profession1)
        {
            //更新

            professionRepository.save(profession1);
        }
        else
        {

            professionRepository.save(profession);
        }

    }

    @Override
    public Page<Profession> selectPage(Integer page, Integer limit, ProfessionQueryVo professionQueryVo) {
        Pageable pageable = PageRequest.of(page-1,limit);
        // 创建Example对象
        Profession profession=new Profession();

        BeanUtils.copyProperties(professionQueryVo,profession);

        //构造条件查询规则
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING)
                .withIgnoreCase(true);

        Example<Profession> example = Example.of(profession,matcher);


        Page<Profession> all = professionRepository.findAll(example,pageable);
        System.out.println(all.toString());
        return all;
    }

    @Override
    public void removeProfession(String lawercode, String professioncode) {
//        Lawer lawer=lawerRepository.findByOfficecodeAndLawercode(officecode,lawercode);
        Profession profession=professionRepository.findByLawercodeAndProfessioncode(lawercode,professioncode);
        if(null!=profession)
        {
            professionRepository.deleteById(profession.getId());
        }
    }


}
